<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    // static
    class Person{
        static num = 10;   // 静态属性
        constructor(name){
            this.name = name;
        }
        static fn(){    // 静态方法
            console.log("fn");
        }
    }

    // 调用静态成员不需要实例化
    console.log(Person.num);
    Person.fn();


    // 统计对象的实例化次数
    // let num = 0;
    class Person1{
        static num = 0;
        constructor(name){
            this.name = name;
            // num++;
            Person1.num++;
        }
    }
    let zhangsan = new Person1("zhangsan");
    let lisi = new Person1("lisi");
    let wangwu = new Person1("wangwu");

    // console.log(num);
    console.log(Person1.num);



    // 单例模式
    // let instance;   // 记录实例化对象
    // class Person2{
    //     constructor(){
    //         this.name = name;
    //         if(instance){
    //             // 实例化过
    //             return instance;
    //         }else{
    //             // 没有实例化过
    //             instance = this;
    //         }
    //     }
    // }
    // let zhangsan1 = new Person2("zhangsan");
    // let lisi1 = new Person2("lisi");
    // console.log(zhangsan1,lisi1);
    
    
    
    class Person3{
            static instance;
            constructor(name){
                this.name = name;
                if(Person3.instance){
                    // 实例化过
                    return Person3.instance;
                }else{
                    // 没有实例化过 ，通过instance 保存实例化对象
                    Person3.instance = this;
                }
            }
        }

        let  zhangsan1 = new Person3("张三");
        let lisi1 = new Person3("李四");
        let wangwu1 = new Person3("王五");
        console.log(zhangsan1,lisi1,wangwu1);


</script>